import { Component } from '@angular/core';
import { IonicPage, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../../providers/http/http';
import { StorageProvider } from '../../../providers/storage/storage';
import { DialogProvider } from '../../../providers/dialog/dialog';
/**
 * Generated class for the ModalsfixedselectPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-modalsfixedselect',
  templateUrl: 'modalsfixedselect.html',
})
export class ModalsfixedselectPage {

  private selectitem: any;
  private multiselect: any;
  private status: any;
  private searchQuery: any;
  private listItems: Array<{
    id: any,
    assetCode: any,
    assetName: any,
    manageDept: any,
    assetBelong: any,
    assetType: any,
    useDept: any,
    usePerson: any,
    place: any,
    status: any,
    statustext: any,
    searched: any
  }>;
  constructor(
    public viewCtrl: ViewController,
    public navParams: NavParams,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider) {
    this.selectitem = this.navParams.get("selectitem"); //页面类型
    this.multiselect = this.navParams.get("multiselect"); //页面类型
    this.status = this.navParams.get("status"); //页面类型
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ModalsfixedselectPage');
    this.loadData();
  }

  loadData() {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id'],
        status: this.status,
        pageNo: 1
      };
      this.http.postJson('oa', 'assets/assetsSelect.do', params).then(res => {
        //返回数据之后,进行查询
        let datas = res['data']['name'];
        this.listItems = new Array();
        for (let i = 0; i < datas.length; i++) {
          let item = datas[i];
          this.listItems.push({
            id: item['id'],
            assetCode: item['assetCode'],
            assetName: item['assetName'],
            manageDept: item['manageDept'],
            assetBelong: item['assetBelong'], 
            assetType: item['assetType'],
            usePerson: item['usePerson'],
            useDept: item['useDept'],
            status: item['status'],
            statustext: item['status']==0?"在库":(item['status']==1?"使用中":(item['status']==2?"已报废":"归还申请中")),
            place: item['place'],
            searched: this.selectitem['id'] != item['id'] ? false : true
          });
        }
        //最终结果
        this.searchQuery = this.listItems;
      });
    });
  }

  //人物id以u_开头
  getItems(ev: any) {
    //输入关键字
    let val = ev.target.value;
    //查询
    if (val && val.trim() != '') {
      this.searchQuery = this.listItems.filter((item) => {
        return (item['assetName'].toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    } else {
      this.searchQuery = this.listItems;
    }
  }

  //关闭选择
  doclose() {
    this.viewCtrl.dismiss();
  }
  //选中操作
  doselect(obj, ev: any) {
    //排他处理
    if (ev && !this.multiselect) {
      //对比
      for (let i = 0; i < this.listItems.length; i++) {
        if (obj['id'] != this.listItems[i]['id']) {
          this.listItems[i]['searched'] = false;
        }
      }
      //部门显示
      for (let i = 0; i < this.searchQuery.length; i++) {
        if (obj['id'] != this.searchQuery['id']) {
          this.searchQuery['searched'] = false;
        }
      }
    }
  }
  //确定选择
  doneselect() {
    //检查选择
    let datas = new Array();
    for (let i = 0; i < this.listItems.length; i++) {
      if (this.listItems[i]['searched'] == true) {
        datas.push(this.listItems[i]);
      }
    }
    //判断选择
    if (datas.length == 0) {
      this.dialog.showAlert('提示', '请选择办公用品');
    } else {
      let data = new Object();
      data['result'] = datas;
      this.viewCtrl.dismiss(data);
    }
  }

}
